#include <mutex>

#include "common/elements/element.h"

#include "item_type.h"

namespace Elements
{

ItemType::ItemType(const ItemType& other)
    : Element(other)
    , m_workflowId(other.m_workflowId)
    , m_defaultStateId(other.m_defaultStateId)
    , m_caption(other.m_caption)
    , m_kind(other.m_kind)
{
}

bool ItemType::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<ItemType const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_workflowId == otherElement->m_workflowId
        && m_defaultStateId == otherElement->m_defaultStateId
        && m_caption == otherElement->m_caption
        && m_kind == otherElement->m_kind
    );
}

Common::Json ItemType::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["workflowId"] = m_workflowId;
    result["defaultStateId"] = m_defaultStateId;
    result["caption"] = m_caption;
    result["kind"] = unsigned(m_kind);
    return result;
}

void ItemType::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_workflowId = json["workflowId"].get<unsigned>();
    m_defaultStateId = json["defaultStateId"].get<unsigned>();
    m_caption = json["caption"].get<std::string>();
    m_kind = ItemType::Kind(json["kind"].get<unsigned>());
}

Element* ItemType::clone() const
{
    return new ItemType(*this);
}

ElementType ItemType::elementType() const
{
    return ElementType::ItemType;
}

unsigned ItemType::workflowId() const
{
    std::shared_lock locker(m_lock);
    return m_workflowId;
}

void ItemType::setWorkflowId(unsigned value)
{
    std::unique_lock locker(m_lock);
    m_workflowId = value;
}

unsigned ItemType::defaultStateId() const
{
    std::shared_lock locker(m_lock);
    return m_defaultStateId;
}

void ItemType::setDefaultStateId(unsigned value)
{
    std::unique_lock locker(m_lock);
    m_defaultStateId = value;
}

std::string ItemType::caption() const
{
    std::shared_lock locker(m_lock);
    return m_caption;
}

void ItemType::setCaption(const std::string& value)
{
    std::unique_lock locker(m_lock);
    m_caption = value;
}

ItemType::Kind ItemType::kind() const
{
    std::shared_lock locker(m_lock);
    return m_kind;
}

void ItemType::setKind(ItemType::Kind value)
{
    std::unique_lock locker(m_lock);
    m_kind = value;
}

} // namespace Elements
